package thread;


public class demo20 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (locker1) {
                System.out.println("t1 拿到了 locker1");

                // 此处的 sleep 目的是让 t1 和 t2 确实都已经拿到各自的 locker1 和 locker2
                // 然后再进行后序操作. 
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2) {
                    System.out.println("t1 拿到了 locker2");

                }
            }

        });
        Thread t2 = new Thread(() -> {
            synchronized (locker2) {
                System.out.println("t2 拿到了 locker1");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker1) {
                    System.out.println("t2 拿到了 locker2");
                }
            }

        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

    }
}